package ldk.xposed.dexreplace

import android.os.Parcel
import android.os.Parcelable
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlSerializer
import java.io.StringWriter

/**
 * Created by liangdengke on 2018/4/25.
 */
class ReplaceDexInfo(val dexFilePath: String, val packageName: String){

    companion object{

        /**
         *   <package name="com.cliff.release11demo">
         *      <dexfilepath>/sdcard/dexreplace/test.dex</dexfilepath>
         *   </package>
         *
         * @param xmlParser: 确保处于正确状态的xmlParser
         */
        fun parseReplaceDexInfo(xmlParser: XmlPullParser): ReplaceDexInfo{
            val packageName = xmlParser.getAttributeValue(null, "name")
            xmlParser.next() // parse package
            xmlParser.next() // parse dexfile
            val dexFilePath = xmlParser.text
            xmlParser.nextTag()
            xmlParser.nextTag()
            return ReplaceDexInfo(dexFilePath, packageName)
        }
    }

    fun writeToXml(xmlSerializer: XmlSerializer){
        xmlSerializer.startTag(null, "package")
        xmlSerializer.attribute(null, "name", packageName)
        xmlSerializer.startTag(null, "dexfilepath")
        xmlSerializer.text(dexFilePath)
        xmlSerializer.endTag(null, "dexfilepath")
        xmlSerializer.endTag(null, "package")
    }

    override fun toString(): String {
        return "ReplaceDexInfo(dexFilePath=$dexFilePath);"
    }
}